﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RoardersAgentSim.Map
{
	/// <summary>
	/// types of link
	/// </summary>
	public enum LinkType
	{
		CAR_LINK,
		WALK_LINK,
	}

	/// <summary>
	/// The links definition
	/// </summary>
	public class Link
	{
		#region private fields
		/// <summary>
		/// The link length
		/// </summary>
		private Double _length;
		/// <summary>
		/// destination node
		/// </summary>
		private Node _destination;
		/// <summary>
		/// source node
		/// </summary>
		private Node _source;
		/// <summary>
		/// Link ID
		/// </summary>
		private string _linkID;


		#endregion
		#region link properties
		/// <summary>
		/// 
		/// </summary>
		public string LinkID
		{
			get { return _linkID; }
			set { _linkID = value; }
		}
		/// <summary>
		/// Determine the link type
		/// </summary>
		public LinkType Type { get; set; }
		/// <summary>
		/// Zone in which the centre of link k is located
		/// </summary>
		public Zone H { get; set; }
		/// <summary>
		/// Link length property
		/// </summary>
		public Double Length
		{
			get { return _length; }
			set { _length = value; }
		}
		#region node specification
		public Node Source
		{
			get { return _source; }
			set { _source = value; }
		}
		public Node Destination
		{
			get { return _destination; }
			set { _destination = value; }
		}
		#endregion

		#region constructor
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="id"></param>
		/// <param name="len"></param>
		/// <param name="type"></param>
		public Link(string id,double len,LinkType type = LinkType.CAR_LINK)
		{
			_linkID = id;
			_length = len;
			Type = type;
		}
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="id"></param>
		/// <param name="len"></param>
		public Link(string id, double len)
		{
			_linkID = id;
			_length = len;
		}
		/// <summary>
		/// Constructor based on the csv information
		/// </summary>
		/// <param name="linkMapping"></param>
		public Link(CSVUtils.CSVMapping.LinkMapping linkMapping)
		{
			
			this._length = linkMapping.Length;
			switch (linkMapping.Type.ToUpper().Trim())
			{
				case "CAR":
					this.Type = LinkType.CAR_LINK;
                    this._linkID = "link" + "CAR"+linkMapping.SourceNode + linkMapping.DestinationNode;
					break;
				case "WALK":
					this.Type = LinkType.WALK_LINK;
                    this._linkID = "link" + "WALK" + linkMapping.SourceNode + linkMapping.DestinationNode;
					break;
				default:
					throw new Exception ("Unrecognized type of link "+linkMapping.Type);
			}
		}
		#endregion

		#region calculation properties
		/// <summary>
		/// travel cost (time) of link k
		/// </summary>
		public double C
		{
			get
			{
				return this.CalculateCost();
			}
		}
		#endregion
		#endregion

		#region private methods
		/// <summary>
		/// Calculate the cost of the link per type
		/// </summary>
		/// <returns></returns>
		private double CalculateCost()
		{
			if (this.Type == LinkType.CAR_LINK)
				return 0;
			return 1;
		}
		#endregion


	}
}
